package cn.huobi.product.service.impl;

import cn.huobi.basic.config.BussinessException;
import cn.huobi.basic.service.impl.BaseServiceImpl;
import cn.huobi.basic.util.CodeGenerateUtils;
import cn.huobi.order.domain.OrderProduct;
import cn.huobi.order.mapper.OrderProductMapper;
import cn.huobi.org.domain.Employee;
import cn.huobi.org.mapper.EmployeeMapper;
import cn.huobi.pet.dto.AcceptOrderDto;
import cn.huobi.product.domain.ProductOrderDetail;
import cn.huobi.product.mapper.ProductOrderDetailMapper;
import cn.huobi.product.service.IProductOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huobi
 * @since 2022-11-03
 */
@Service
public class ProductOrderDetailServiceImpl extends BaseServiceImpl<ProductOrderDetail> implements IProductOrderDetailService {


    @Autowired
    private ProductOrderDetailMapper productOrderDetailMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private OrderProductMapper orderProductMapper;

    @Override
    public void accept(AcceptOrderDto acceptOrderDto) {
        // 根据id查询服务
        ProductOrderDetail productOrderDetail = productOrderDetailMapper.findById(acceptOrderDto.getMsgId());
        // 判断寻主消息是否为空
        if (productOrderDetail == null){
            throw new BussinessException("服务为空，请重新操作");
        }
        // 解决线程安全问题
        synchronized (productOrderDetail.getId()){
            Employee employee = employeeMapper.findById(acceptOrderDto.getHandler());
            if (employee == null){
                throw new BussinessException("不存在此员工，请重新操作");
            }
            // 修改寻主消息状态
            if (productOrderDetail.getState() == 0 && employee.getShopId().equals(productOrderDetail.getShopId())){
                productOrderDetail.setState(1L);
                productOrderDetailMapper.update(productOrderDetail);
                // 生成收购单
                initOrderProduct(acceptOrderDto, productOrderDetail, employee);
            }
        }
    }

    @Override
    public void reject(Long msgId, Employee employee) {
        // 根据id查询
        ProductOrderDetail productOrderDetail = productOrderDetailMapper.findById(msgId);
        // 判断寻主消息是否为空
        if (productOrderDetail == null){
            throw new BussinessException("订单消息为空，请重新操作");
        }
        // 将寻主消息shopId置空
        productOrderDetail.setShopId(null);
        productOrderDetailMapper.update(productOrderDetail);
    }

    private void initOrderProduct(AcceptOrderDto acceptOrderDto, ProductOrderDetail productOrderDetail, Employee employee) {
        OrderProduct orderProduct = new OrderProduct();
        orderProduct.setDigest(acceptOrderDto.getNote());
        orderProduct.setState(0);
        orderProduct.setPrice(productOrderDetail.getPrice());
        orderProduct.setOrderSn(CodeGenerateUtils.generateOrderSn(acceptOrderDto.getHandler()));
        orderProduct.setProductId(productOrderDetail.getProductId());
        orderProduct.setUserId(productOrderDetail.getUserId());
        orderProduct.setShopId(productOrderDetail.getShopId());
        orderProduct.setEmpId(employee.getId());
        orderProductMapper.add(orderProduct);

    }
}
